﻿<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Hello Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class DeTaiModelDeTai extends JModel
{
	 var $_allData;
	/**
	 * Constructor that retrieves the ID from the request
	 *
	 * @access	public
	 * @return	void
	 */
	 function __construct()
	  {
		parent::__construct();
	 
		global $mainframe, $option;
	 
		// Get pagination request variables
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
	 
		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
	 
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
		
		
		$filter_order     = $mainframe->getUserStateFromRequest(  $option.'filter_order', 'filter_order', 'default_column_name', 'cmd' );
		$filter_order_Dir = $mainframe->getUserStateFromRequest( $option.'filter_order_Dir', 'filter_order_Dir', 'asc', 'word' );
		$filter_chuyennganh = $mainframe->getUserStateFromRequest( $option.'filter_chuyennganh', 'filter_chuyennganh', '', 'string' );
		$filter_nganh = $mainframe->getUserStateFromRequest( $option.'filter_nganh', 'filter_nganh', '', 'string' );
		
		$this->setState('filter_chuyennganh',$filter_chuyennganh);
		$this->setState('filter_nganh',$filter_nganh);
		
		$this->setState('filter_order', $filter_order);
		$this->setState('filter_order_Dir', $filter_order_Dir);
	  }
	  function _buildContentOrderBy()
	{
		global $mainframe, $option;
	 
			$orderby = '';
			$filter_order     = $this->getState('filter_order');
			$filter_order_Dir = $this->getState('filter_order_Dir');
	 
			/* Error handling is never a bad thing*/
			if(!empty($filter_order) && !empty($filter_order_Dir) ){
				$orderby = ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
			}

			return $orderby;
	}
	
	 function getTotal()
	  {
		// Load the content if it doesn't already exist
		if (empty($this->_total)) {
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);	
		}
		return $this->_total;
	  }
	  
	function getPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination)) {
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_pagination;
  }
	
	function getAllData() 
	{
		// if data hasn't already been obtained, load it
		if (empty($this->_data)) {
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));	
		}
		return $this->_data;
	}
	function _getWhere()
	{
		$where=array();
		$manganh=$this->getState('filter_nganh');
		$chuyennganh=$this->getState('filter_chuyennganh');
		if($manganh !='')
			$where[]='manganh = \''.$manganh.'\''; 
		if($chuyennganh !='')
			$where[]='tenchuyennganh = \''.$chuyennganh.'\''; 
		$tendetai=JRequest::getVar('tendetai','');
		if($tendetai !='')
			$where[]='tendetai like \'%'.$tendetai.'%\''; 
		$where = ( count( $where ) ? ' WHERE (' . implode( ') AND (', $where ) . ')' : '' );
		return $where;
	}
	function _buildQuery() 
	{ 
		$query = ' SELECT * ' 
			   . ' FROM #__de_tai '.$this->_getWhere(); 
		; 
		return $query; 
	} 
	/* function getAllData() 
	{ 
		// Lets load the data if it doesn't already exist 
		if (empty( $this->_allData )) 
		{ 
			$query = $this->_buildQuery(); 
			$this->_allData = $this->_getList( $query ); 
		} 
		return $this->_allData; 
	} */
	
	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{	

		$madetai = JRequest::getVar('madetai','','post');
		$tendetai = JRequest::getVar('tendetai','','post');
		$manganh= JRequest::getVar('filter_nganh','','post');
		$tenphannganh= JRequest::getVar('tenphannganh','','post');
		$text = JRequest::getVar('text','','post');
		if($text=="New")
		{
			$query="Insert into #__de_tai (madetai,tendetai,manganh,tenchuyennganh) values('".$madetai."','".$tendetai ."','".$manganh."','".$tenphannganh."')";
		}
		else
		{			
			$query="Update #__de_tai set tendetai ='".$tendetai."' , manganh='".$manganh."' , tenchuyennganh='".$tenphannganh."' where madetai='".$madetai."'" ;
		}
		$db=& JFactory::getDBO();
		$db->setQuery($query);
		if(! $db->query())
		{
			return false;
		}
		return true;
	}

	/**
	 * Method to delete record(s)
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function delete()
	{
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$db=& JFactory::getDBO();
		$i=0;
		if (count( $cids )) {
			foreach($cids as $cid) {
				$sql="Delete from #__de_tai where madetai='".$cid."'";
				$db->setQuery($sql);
				if (!$db->query()) {
					return false;
				}
			}
		}
		return true;
	}
}